package co.fitstart.mobile.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.mobile.service.CouponService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;

/**
 * 
 * @author lidan
 *
 */
@Controller
@RequestMapping("")
public class CouponClaimController {
    
    private static final Logger logger = LoggerFactory.getLogger(CouponClaimController.class);
    
    @Value("#{config['wheely.coupon.ids']}")
    private String wheelyCouponIdsString;
    
    @Autowired
    private CouponService couponService;
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/wheely-coupons", method = RequestMethod.GET)
    @Router
    @MobileDetect(autoLogin = false)
    public ModelAndView drawWheelyCoupon() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/wheely-coupons-claiming");
        
        List<CouponBatch> batches = couponService.listCouponBatch(this.getBatchId(wheelyCouponIdsString));
        
        modelAndView.addObject("batches", batches);
            
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/wheely-coupons", method = RequestMethod.POST)
    public @ResponseBody String onDrawWheelyCoupon(@RequestParam("mobile") String mobile) {
        
        Result result = new Result();
        
        try {
            
            List<CouponBatch> batches = couponService.listCouponBatch(this.getBatchId(wheelyCouponIdsString));
            couponService.createCoupons(mobile, batches);
            result.success().data("url", "/wheely-coupons/claimed").message("领取优惠券成功");
            
        } catch (Exception e) {
            
            logger.error("drawCoupon error:", e);
            result.fail().message("领取优惠券失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/wheely-coupons/claimed", method = RequestMethod.GET)
    @Router
    @MobileDetect(autoLogin = false)
    public ModelAndView wheelyCouponClaimed() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/wheely-coupons-claimed");
        
        List<CouponBatch> batches = couponService.listCouponBatch(this.getBatchId(wheelyCouponIdsString));
        modelAndView.addObject("batches", batches);
            
        return modelAndView;
    }
    
    /**
     * 
     * @param couponIdsString
     * @return
     */
    protected List<Long> getBatchId(String couponIdsString) {
        String[] couponIdsArray = StringUtils.split(couponIdsString, ",");
        
        List<Long> batchhIds = new ArrayList<Long>(couponIdsArray.length);
        for(String couponIdString : couponIdsArray) {
            batchhIds.add(Long.parseLong(couponIdString));
        }
        return batchhIds;
    }

}
